"use strict";

var page = 1,
    defulSize = 10,
    catalIndex = 1,
    allPage = void 0,
    searchObj = void 0;
toastr.options = {
  closeButton: false,
  debug: false,
  progressBar: true,
  positionClass: "toast-top-center",
  onclick: null,
  showDuration: "300",
  hideDuration: "1000",
  timeOut: "2000",
  extendedTimeOut: "1000",
  showEasing: "swing",
  hideEasing: "linear",
  showMethod: "fadeIn",
  hideMethod: "fadeOut"
};
/**
 * 页面初始化时加载 window.onload
 */
$(function () {
  //拿到页面参数
  var options = geeurlcodeObj();
  if (options.val) {
    loadNews(options.val);
    $("#search_Inp").val(options.val);
  }
  $(".list-box .n-box").click(function () {
    $(".list-box .n-box").removeClass("active");
    $(this).addClass("active");
    var type = $(this).attr("data");
    var val = $("#search_Inp").val();
    if (!val) {
      return toastr.info("请输入关键字");
    }
    loadNews(val, type);
  });
});

//查找
function serchFunction() {
  var val = $("#search_Inp").val();
  if (!val) {
    return toastr.info("请输入关键字");
  }
  loadNews(val);
}

//根据关键字加载新闻
function loadNews(val, type) {
  var params = {
    keyWord: val
  };
  if (type) {
    params.type = type;
  }
  searchObj = params;
  params.pageNum = page;
  params.pageSize = defulSize;

  $.server(params, '/search/v1/aggSearch').then(function (res) {
    var obj = res.result.data;
    var html = '';
    $(".infoNum").text(res.result.total);
    if (obj.length == 0) {
      $("#b_box").html("抱歉,没有找到相关内容,请更换查询条件重新搜索");
      return;
    }
    for (var i = 0; i < obj.length; i++) {
      var imghtml = "<img src='images/infoDetail/infoDetail-5.png'>";
      if (obj[i].contentType == "content") {
        if (obj[i].type == "1") {
          imghtml = "<img src='images/infoDetail/zx_active.png'>";
        } else if (obj[i].type == "2") {
          imghtml = "<img src='images/infoDetail/fg_active.png'>";
        } else if (obj[i].type == "3") {
          imghtml = "<img src='images/infoDetail/bg_active.png'>";
        } else if (obj[i].type == "4") {
          imghtml = "<img src='images/infoDetail/bg_active.png'>";
        }
      } else if (obj[i].contentType == "lecturer") {
        imghtml = "<img src='images/infoDetail/zj_active.png'>";
      } else if (obj[i].contentType == "course") {
        imghtml = "<img src='images/infoDetail/kc_active.png'>";
      }

      if (obj[i].poster) {
        html += "<div class=\"info-box\" onclick=\"goNewsDetail(" + obj[i].searchId + "," + obj[i].type + ",'" + obj[i].contentType + "')\">\n                      <div class=\"title-box\">\n                          " + imghtml + "\n                          <div class=\"title\">" + obj[i].title + "</div>\n                      </div>\n                      <div class=\"img-title-box\">\n                          <img src='" + obj[i].poster + "' />\n                          <div class=\"content\">" + obj[i].content + "</div>\n                      </div>\n                  </div>";
      } else {
        //没有主图  纯内容
        html += "<div class=\"info-box\" onclick=\"goNewsDetail(" + obj[i].searchId + "," + obj[i].type + ",'" + obj[i].contentType + "')\">\n                      <div class=\"title-box\">\n                        " + imghtml + "\n                          <div class=\"title\">" + obj[i].title + "</div>\n                      </div>\n                      <div class=\"content\">" + obj[i].content + "</div>\n                  </div>";
      }
    }
    $("#b_box").html(html);
    allPage = res.result.totalPage;
    getPageNav(res.result.totalPage);
  });
}

function goNewsDetail(id, type, contentType) {
  if (contentType == "content") {
    //
    location.href = "industryDetail.html?id=" + id + "&word=聚合搜索&childTitle=资讯详情";
  } else if (contentType == "course") {
    //课程
    location.href = "trainDetail.html?id=" + id;
  } else if (contentType == "lecturer") {
    //专家
    location.href = "expert.html?id=" + id;
  } else {
    location.href = "industryDetail.html?id=" + id + "&word=聚合搜索&childTitle=资讯详情";
  }
}

// 计算分页 点击培训分类时  页面加载时都要调用
function getPageNav(totalPage) {
  var html = '';
  // const len = Math.ceil(Number(length) / Number(page));
  if(totalPage>10 && page>3){
    html+='...'
  }
  if(totalPage>10){
    for (var i = 0; i < totalPage; i++) {
      if( i >= page-3 && i< page+2){
        if(i+1 == page){
          html += "<div class='pageBox active' onclick= reloadNews(" + (i + 1) + ") > " + (i + 1) + " </div>";
        }else{
          html += "<div class=\"pageBox\" onclick= reloadNews(" + (i + 1) + ") > " + (i + 1) + " </div>";
        }
        
      } 
    }
    if(page<totalPage-3){
      html+='...'
    }
   
    if(page<totalPage-3){
      html += "<div class='pageBox' onclick= reloadNews(" + (totalPage) + ") > " + (totalPage) + " </div>";
    }
    $("#pageSizeBox").html(html);
  }else{
    for (var i = 0; i < totalPage; i++) {
      html += "<div class=\"pageBox\" onclick= reloadNews(" + (i + 1) + ") > " + (i + 1) + " </div>";
    }
    $("#pageSizeBox").html(html);
    $("#pageSizeBox>div").eq(page - 1).addClass("active");
  }
  
  
 
}

//上一页
function prevPage() {
  if (page == 1) {
    return toastr.info("没有上一页了喔~");
  }
  page--;
  loadNews(searchObj.keyWord, searchObj.type);
}
//下一页
function nextPage() {
  if (page >= allPage) {
    return toastr.info("没有下一页了喔~");
  }
  page++;
  loadNews(searchObj.keyWord, searchObj.type);
}

//首页
function firstPage() {
  if (page == 1) {
    return toastr.info("已经在首页了喔~");
  }
  page = 1;
  loadNews(searchObj.keyWord, searchObj.type);
}

//尾页
function lastPage() {
  if (page == allPage) {
    return toastr.info("已经在尾页了喔~");
  }
  page = allPage;
  loadNews(searchObj.keyWord, searchObj.type);
}

// 点击GO进入任一页面
function goPage() {
  var val = $("#pageVal").val();
  if (!val || val <= 0 || val > allPage) {
    return toastr.info("不存在的页面~");
  }
  if (val == page) {
    return;
  }
  page = val;
  loadNews(searchObj.keyWord, searchObj.type);
}
//点击分页重新加载
function reloadNews(i) {
  page = i;
  loadNews(searchObj.keyWord, searchObj.type);
}
